package com.pengliu.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;


import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.pengliu.common.bean.SysAuth;
import com.pengliu.common.bean.SysAuthExample;
import com.pengliu.common.bean.SysRole;
import com.pengliu.common.bean.SysUser;
import com.pengliu.common.bean.SysUserExample;
import com.pengliu.common.exception.ServiceException;
import com.pengliu.config.security.SecurityDetails;
import com.pengliu.dao.SysAuthMapper;
import com.pengliu.dao.SysRoleMapper;
import com.pengliu.dao.SysUserMapper;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	@Autowired
	private SysAuthMapper sysAuthMapper;
	
	@Autowired
	private SysRoleMapper sysRoleMapper;
	
	/**
     * 覆写此方法 sc框架会调用此方法完成用户认证的第一步
     *
     * @param username
     * @return UserDetails 返回 不带权限资源集合
     * @throws UsernameNotFoundException
     */
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		SysUserExample example = new SysUserExample();
		example.getCriteria()
			.andUsernameEqualTo(username);
		List<SysUser> listUsers = sysUserMapper.selectByExample(example);
		
		SysUser sysUser = null;
		
		if(!listUsers.isEmpty()) {
			sysUser = listUsers.get(listUsers.size()-1);
		}
		
		if(null == sysUser) {
			throw new UsernameNotFoundException("用户名不存在");
		}
		
		SecurityDetails securityDetails = new SecurityDetails();
		BeanUtils.copyProperties(sysUser, securityDetails);
		securityDetails.setEnabled(sysUser.getIsEnabled());
		
		//根据用户ID获取权限集合
		List<SysAuth> listSysAuth = sysAuthMapper.findBySysUserId(sysUser.getId());
		securityDetails.setListSysAuth(listSysAuth);
		Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>();
		for (SysAuth sysAuth : listSysAuth) {
			if (null != sysAuth && sysAuth.getName()!=null) {
				GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(sysAuth.getName());
				grantedAuthorities.add(grantedAuthority);
			}
		}
		securityDetails.setAuthorities(grantedAuthorities);
		
		//根据用户ID获取角色
		List<SysRole> listRoles = sysRoleMapper.findBySysUserId(sysUser.getId());
		SysRole sysRole = null;
		if(!listRoles.isEmpty()) {
			sysRole = listRoles.get(listRoles.size()-1);
		}
		securityDetails.setSysRole(sysRole);
		
		return securityDetails;
	}
	
	/**
	 * user: pengliu
	 * date: 2017年9月27日
	 * desc: 获取所有权限信息
	 * @return
	 */
	public List<SysAuth> getSysAuthAll(){
		SysAuthExample example = new SysAuthExample();
		return sysAuthMapper.selectByExample(example);
	}
}
